package com.bocaizp.operation.controller.user;

import com.bocaizp.common.context.BaseContext;
import com.bocaizp.common.result.Result;
import com.bocaizp.common.utils.AliOssUtil;
import com.bocaizp.operation.mapper.LoginMapper;
import com.bocaizp.operation.mapper.UserInfoMapper;
import com.bocaizp.operation.service.UserInfoService;
import com.bocaizp.pojo.DTO.ModifiedInfoDTO;
import com.bocaizp.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * Author : huang jie
 * Date : 2024/5/7 17:52
 **/
@RestController
@RequestMapping("/user")
@Slf4j
@CrossOrigin
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private AliOssUtil aliOssUtil;

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private LoginMapper loginMapper;

	/**
	 * 修改个人信息
	 * @param modifiedInfoDTO
	 * @return
	 */
	@PostMapping("/modified/info")
	public Result modidiedInfo(@RequestBody ModifiedInfoDTO modifiedInfoDTO){
		log.info("修改个人信息请求...{}",modifiedInfoDTO);
		userInfoService.modifiedInfo(modifiedInfoDTO);
		return Result.success();
	}

	/**
	 * 上传文件（oss）
	 * @param file
	 * @return
	 */
	@PostMapping("/upload")
	public Result<String> upload(MultipartFile file){
		log.info("文件上传 {}",file);
		int flag = 0;
		userInfoService.delete(flag);
		try {
			String originalFilename = file.getOriginalFilename();//获取原始文件名称
			//以字符串最后一个.截断得到文件后缀
			String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
			//利用UUID与文件后缀结合成一个新的不重复的文件名
			String object = UUID.randomUUID() + extension;
			String path = aliOssUtil.upload(file.getBytes(), object);
			userInfoMapper.updateAvatar(path, BaseContext.getCurrentId());
			return Result.success(path);
		} catch (IOException e) {
			log.error("文件上传失败 :{}",e);
		}
		return Result.success();
	}

	/**
	 * 上传简历（oss）
	 * @param file
	 * @return
	 */
	@PostMapping("/upload/bio")
	public Result<String> uploadBid(MultipartFile file){
		log.info("简历上传 {}",file);
		int flag = 1;

		try {
			String originalFilename = file.getOriginalFilename();//获取原始文件名称
			//以字符串最后一个.截断得到文件后缀
			String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
			//利用UUID与文件后缀结合成一个新的不重复的文件名
			if(!".pdf".equals(extension)){
				return Result.error("请上传PDF格式文件！");
			}
			String object = UUID.randomUUID() + extension;
			userInfoService.delete(flag);
			String path = aliOssUtil.upload(file.getBytes(), object);
			userInfoMapper.updateBio(path, BaseContext.getCurrentId());
			return Result.success(path);
		} catch (IOException e) {
			log.error("文件上传失败 :{}",e);
		}
		return null;
	}

	/**
	 * 修改密码
	 * @param password
	 * @return
	 */
	@GetMapping("/modified/password")
	public Result changePassword(String password){
		log.info("修改密码请求...{}",password);
		Long id = BaseContext.getCurrentId();
		userInfoMapper.updatePassword(password, id);
		return Result.success();
	}

	/**
	 * 修改个人意向
	 * @param user
	 * @return
	 */
	@PostMapping("/modified/expect")
	public Result modifiedExpect(@RequestBody User user){
		log.info("修改个人意向请求...");
		user.setId(BaseContext.getCurrentId());
		loginMapper.update(user);
		return Result.success();
	}
}
